package org.coco24.pmstudio.converter

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.utils.Array
import com.badlogic.gdx.utils.JsonReader
import com.badlogic.gdx.utils.JsonWriter

object Converter
{
    fun p1()
    {
        val array = com.badlogic.gdx.utils.Array<Pair<String, String>>()
        val reader = JsonReader()
        val value = reader.parse(Gdx.files.internal("data/moves_1.json").reader("UTF-8"))
        value.forEach {
            array.add(Pair(it.name(), it.asString()))
        }
        val jsonWriter = JsonWriter(Gdx.files.local("data/moves.json").writer(false))
        val root = jsonWriter.`object`()
        array.forEach {
            root.`object`(handleInternalName(it.first)).`object`("name").set("zh_CN", it.second).set("en", it.first).pop().pop()

        }
        jsonWriter.flush()
        jsonWriter.close()
    }
    fun p2()
    {
        val array = com.badlogic.gdx.utils.Array<Pair<String, String>>()
        val reader = JsonReader()
        val value = reader.parse(Gdx.files.internal("data/egggroups.json").reader("UTF-8"))
        value.forEach {
            array.add(Pair(it.name(), it.asString()))
        }
        val jsonWriter = JsonWriter(Gdx.files.local("data/egggroups_1.json").writer(false))
        val root = jsonWriter.`object`()
        array.forEach {
            root.`object`(it.second.toUpperCase().replace(' ', '_')).`object`("name").set("zh_CN", it.first).set("en", it.second).pop().pop()

        }
        jsonWriter.flush()
        jsonWriter.close()
    }
    fun handleInternalName(name: String) = name.toUpperCase().replace(' ', '_')
    fun p3()
    {
        val reader = JsonReader()
        val value = reader.parse(Gdx.files.internal("data/types_1.json").reader("UTF-8"))
        val jsonWriter = JsonWriter(Gdx.files.local("data/types.json").writer(false))
        val root = jsonWriter.`object`()
        value.forEach {
            jsonWriter.`object`(handleInternalName(it.getString("english"))).`object`("name")
                    .set("zh_CN", it.getString("chinese"))
                    .set("en", it.getString("english"))
                    .set("jp", it.getString("japanese"))
                    .pop().pop()
        }
        jsonWriter.flush()
        jsonWriter.close()
    }
    fun p4()
    {
        val a1 = Gdx.files.internal("data/a").readString().split("\n\n")
        val jsonWriter = JsonWriter(Gdx.files.local("data/evolutions.json").writer(false))
        val root = jsonWriter.`object`()
        a1.forEach {
            val it = it.split('\n')
            val o = root.`object`("").`object`("name").set("zh_CN", it[0]).set("en", "").pop().`object`("value")
            for(i in 1 until it.size)
            {
                o.`object`("").`object`("name").set("zh_CN", it[i]).set("en", "").pop().set("value", "INT").pop()
            }
            root.pop().pop()
        }
        jsonWriter.flush()
        jsonWriter.close()
    }
}